import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    alias: '/home',
    name: 'home',
    component: () => import('../views/Home'),
    meta: {
      title: '首页',
      keepAlive: true,
      tx: 1
    }
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: () => import('../views/Detail'),
    meta: {
      title: '详情',
      tx: 2
    }
  },
  {
    path: '/comment/:id',
    name: 'comment',
    component: () => import('../views/Comment'),
    meta: {
      title: '评论',
      tx: 3
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login'),
    meta: {
      title: '登录',
      tx: 2
    }
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User'),
    meta: {
      title: '个人中心',
      tx: 2
    }
  },
  {
    path: '*',
    name: '404',
    component: () => import('../views/Home'),
    meta: {
      title: '404',
      tx: 2
    }
  },
]
// window.history.length > 1 ? this.$router.back() : this.$router.push("/");
const scrollBehavior = function (to, from, savedPosition) {
  // savedPosition 会在你使用浏览器前进或后退按钮时候生效
  // 这个跟你使用 router.go() 或 router.back() 效果一致
  // 这也是为什么我在 tab 栏结构中放入了一个 点击回退 的按钮
  if (savedPosition) {
    return savedPosition
  } else {
    // 如果不是通过上述行为切换组件，就会让页面回到顶部
    // return {x: 0, y: 0}
    return savedPosition
  }
}

const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes,
  // scrollBehavior
})

export default router
